package test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import com.sun.org.apache.bcel.internal.generic.GETSTATIC;

public class MonEval {
    static float sommeTotale5=0;
    static float moyenne5=0;
    static float sommeTotale10=0;
    static float moyenne10=0;
    static float sommeTotale25=0;
    static float moyenne25=0;

	public static Hashtable<String, String> lireQrel(InputStream ips2) throws NumberFormatException, IOException{
		Hashtable<String, String> qrel = new Hashtable<String, String>();
		InputStreamReader ipsr2=new InputStreamReader(ips2);
		BufferedReader br2=new BufferedReader(ipsr2);
		String ligne2;
		while ((ligne2=br2.readLine())!=null){
			
			String[] nomQrel = ligne2.split("\\t");
		
			if(nomQrel[1].equals("0,5") || nomQrel[1].equals("1")) {
				//qrel.put("corpus\\"+nomQrel[0], nomQrel[1]);
				qrel.put("corpus/"+nomQrel[0], nomQrel[1]);
			}
		}
		
		return qrel;
	}
	
	public static  void EvalOne(String fichierSortie, String fichierQrel) {
		Hashtable<String, String> qrel;
		try{
			InputStream ips=new FileInputStream(fichierSortie); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			
			InputStream ips2=new FileInputStream(fichierQrel); 
			float cpt=0;
			float precision=0;
			float pre5=0;
			float pre10=0;
			float pre25=0;
			
			qrel = lireQrel(ips2);
			
			for (int i=0; i<30; i++){
				ligne = br.readLine();
				if (ligne != null){		
					String[] req = null;
					req = ligne.split("\\t");
					
					if (qrel.containsKey(req[0])){
						cpt++;
					}
					
					precision = cpt/(i+1);
					
					if (i==4){
						pre5=precision;
						sommeTotale5=sommeTotale5+pre5;
						System.out.println("precision a 5: "+pre5);	
					}
					if (i==9){
						pre10=precision;
						sommeTotale10=sommeTotale10+pre10;
						System.out.println("precision a 10: "+pre10);
					}
					if (i==24){
						pre25=precision;
						sommeTotale25=sommeTotale25+pre25;
						System.out.println("precision a 25: "+pre25);
					}
				}	
			}
			br.close(); 
			
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
	}
	
	public static void main(String argv[])  {
		
		// Definir nombre de requetes (pour SRI Classique: 10 requetes traitées, SPARQL : 9 requetes traitées)
		int nbReq = 9;

		for (int i=1; i<=nbReq; i++){
			System.out.println("Calculs concernant le qrelQ"+i);
			String sortieTest ="src/fichSortie"+i+".txt";
			String qrelTest ="qrels/qrelQ"+i+".txt";
			MonEval.EvalOne(sortieTest, qrelTest);
			System.out.println("\t");
		}
		
		System.out.println("------------------------------");
		
		moyenne5=sommeTotale5/10;
		System.out.println("la moyenne à 5: "+moyenne5);
		System.out.println("\t");
		moyenne10=sommeTotale10/10;
		System.out.println("la moyenne à 10: "+moyenne10);
		System.out.println("\t");
		moyenne25=sommeTotale25/10;
		System.out.println("la moyenne à 25: "+moyenne25);
	}

}
